import { IRouterContext } from "koa-router";
import { sql } from "../util/db";
import * as config from "config";
import { getImgurl } from "../util/imge";


export const create = async (ctx: IRouterContext | any) => {
    const body = ctx.req.body || ctx.request.body;
    let picurls = [];
    const files = ctx.req.files;
    if (files) {
        picurls = files.map((file: any) => {
            return file.filename;
        });
    }
    if (!body.user_id) {
        return ctx.status = 400;
    }
    let topic_id;
    const text = `insert into t_topic(community_id, user_id, text, picurls, notice)values($1, $2, $3, $4, $5) returning id`;
    const params = [body.community_id, body.user_id, body.text, JSON.stringify(picurls), body.notice];
    const { rows } = await sql.query(text, params);
    ctx.body = topic_id;
}

export const findByCommunityId = async (ctx: IRouterContext) => {
    const community_id = ctx.query.community_id;
    const user_id = ctx.query.user_id;
    const text = `select t.id, t.community_id, t.user_id, t.text, t.picurls, t.notice, t.created_at
                    from t_topic t
                    where t.community_id=$1 order by created_at desc`;
    const res = await sql.query(text, [community_id]);
    const topics = res.rows;

    const text2 = `SELECT openid, headimgurl FROM t_user t1 join t_topic_liker t2 on t1.openid=t2.user_id  WHERE t2.topic_id=$1`;
    const text3 = `SELECT n.received
                    FROM t_topic t
                      JOIN t_topic_notice n ON (t.id = n.topic_id)
                    WHERE t.community_id = $1 AND t.id = $2 AND t.notice = TRUE AND n.user_id = $3;`
    for (let topic of topics) {
        topic.headimgurl = getImgurl(topic.headimgurl);
        topic.picurls = topic.picurls.map((picurl: string) => getImgurl(picurl));
        const res2 = await sql.query(text2, [JSON.stringify(topic.id)]);
        const res3 = await sql.query(text3, [community_id, topic.id, user_id]);
        if (res3.rows[0] && res3.rows[0].received) {
            topic.received_notice = true;
        }
        topic.likers = res2.rows;
        topic.likers = topic.likers.map((liker: any) => {
            liker.headimgurl = getImgurl(liker.headimgurl);
            return liker;
        });
    }
    ctx.body = topics;
}

export const createComment = async (ctx: IRouterContext) => {
    const topic_id = ctx.params.id;
    const body = ctx.request.body;
    const text = `insert into t_topic_comment(topic_id, user_id, text)values($1, $2, $3) returning id`;
    const params = [topic_id, body.user_id, body.text];
    const { rows } = await sql.query(text, params);
    ctx.body = rows[0].id;
}

export const findCommentsByTopicId = async (ctx: IRouterContext) => {
    const topic_id = ctx.params.id;
    const text = `SELECT
                    c.id,
                    c.topic_id,
                    c.user_id,
                    u.headimgurl,
                    u.nickname,
                    c.text,
                    c.created_at
                  FROM t_topic_comment c JOIN t_user u ON c.user_id=u.openid
                  WHERE topic_id = $1 order by created_at desc`;
    const res = await sql.query(text, [topic_id]);
    const comments = res.rows;

    const text2 = `SELECT openid, headimgurl FROM t_user t1 join t_topic_comment_liker t2 on t1.openid=t2.user_id  WHERE t2.comment_id=$1`;
    for (let comment of comments) {
        comment.headimgurl = getImgurl(comment.headimgurl);
        const res2 = await sql.query(text2, [comment.id]);
        comment.likers = res2.rows;
        for (const liker of comment.likers) {
            liker.headimgurl = getImgurl(liker.headimgurl);
        }
    }
    ctx.body = comments;
}

export const createLiker = async (ctx: IRouterContext) => {
    const topic_id = ctx.params.id;
    const liker_id = ctx.params.liker_id;
    const text = `insert into t_topic_liker(topic_id, user_id)values($1, $2)`;
    const { rowCount } = await sql.query(text, [topic_id, liker_id]);
    ctx.body = rowCount;
}

export const createCommentLiker = async (ctx: IRouterContext) => {
    const comment_id = ctx.params.comment_id;
    const user_id = ctx.params.liker_id;
    const text = `insert into t_topic_comment_liker(comment_id, user_id)values($1, $2)`;
    const { rowCount } = await sql.query(text, [comment_id, user_id]);
    ctx.body = rowCount;
}

export const removeLiker = async (ctx: IRouterContext) => {
    const topic_id = ctx.params.id;
    const user_id = ctx.params.liker_id;
    const text = `delete from t_topic_liker where topic_id=$1 and user_id=$2`;
    const { rowCount } = await sql.query(text, [topic_id, user_id]);
    ctx.body = rowCount;
}

export const removeCommentLiker = async (ctx: IRouterContext) => {
    const comment_id = ctx.params.comment_id;
    const user_id = ctx.params.liker_id;
    const text = `delete from t_topic_comment_liker where comment_id=$1 and user_id=$2`;
    const { rowCount } = await sql.query(text, [comment_id, user_id]);
    ctx.body = rowCount;
}

export const createReceiveNotice = async (ctx: IRouterContext) => {
    const topic_id = ctx.params.id;
    const user_id = ctx.request.body.user_id;
    const text = `insert into t_topic_notice(topic_id, user_id, received)values($1, $2, $3)`;
    const { rowCount } = await sql.query(text, [topic_id, user_id, true]);
    ctx.body = rowCount;
}